package com.edu.koTA07.takingOrder;

//http://www.mail-archive.com/android-developers@googlegroups.com/msg82386.html

//import android.R.array;
import java.util.Arrays;
import java.util.Locale;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LogIn extends Activity {
        public static final String DEBUG_TAG = "FullDatabase Log";
        public static final int DB_version = 1;
        //public static final String DB_name = "User.db";

        public static final String TABLE_NAME = "tbl_users";
        //public static final String TABLE_NAME = "tbl_temp";
        //public static final String DB_name = "User_temp";
        public static final String DB_name = "User";
        public static final String CREATE_USER_TABLE = "CREATE TABLE tbl_users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, password TEXT);";
        public static final String DROP_USER_TABLE = "DROP TABLE tbl_users;";
        public DatabaseHelper database_helper;
        public SQLiteDatabase database;
        public SQLiteDatabase database_rd;
        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        Toast.makeText(this,"Before Bundled", Toast.LENGTH_SHORT).show();
        createDataBase();

        //LogIn login = new LogIn();
        Button submit = (Button)findViewById(R.id.submitlogin);
        Toast.makeText(this,"after Bundled", Toast.LENGTH_SHORT).show();

        submit.setOnClickListener(new View.OnClickListener()
        {

                        public void onClick(View v) {
                                // TODO Auto-generated method stub

                                LogIn login = new LogIn();
                                
                                Toast.makeText(login.getApplicationContext(),"In Helper executing onCreate()", Toast.LENGTH_SHORT).show();

                                String[] success = insertRecords();
                                if(success[0].equalsIgnoreCase("1"))
                                {
                                        Intent mIntent = new Intent(LogIn.this,DisplayUser.class);                                    
                                        mIntent.putExtra("username",success[1]);
                                        mIntent.putExtra("password",success[2]);
                                        startActivity(mIntent);
                                }

                        }

                });

        //database.close();
    }

    public void LogCursorInfo(Cursor c)
    {
                Log.i(DEBUG_TAG, "*** Cursor Begin *** " + " Results:" + c.getCount() + " Columns: " + c.getColumnCount());

                // Print column names
                String rowHeaders = "|| ";
                for (int i = 0; i < c.getColumnCount(); i++) {

                        rowHeaders = rowHeaders.concat(c.getColumnName(i) + " || ");
                }
                Log.i(DEBUG_TAG, "COLUMNS " + rowHeaders);
                //Integer rows = c.getCount();
                //Toast.makeText(this, rows.toString(), Toast.LENGTH_SHORT).show();
                // Print records
                c.moveToFirst();
                while (c.isAfterLast() == false) {
                        String rowResults = "|| ";
                        for (int i = 0; i < c.getColumnCount(); i++) {
                                rowResults = rowResults.concat(c.getString(i) + " || ");
                        }
                        Log.i(DEBUG_TAG, "Row " + c.getPosition() + ": " + rowResults);
                        Toast.makeText(this, rowResults.toString(),Toast.LENGTH_SHORT).show();
                        //Toast.makeText(this, "In LogCursorInfo", Toast.LENGTH_SHORT).show();
                        c.moveToNext();
                }
                Log.i(DEBUG_TAG, "*** Cursor End ***");
        }

    void createDataBase()
    {
        if(Arrays.binarySearch(databaseList(), DB_name)>=0)
        {
                deleteDatabase(DB_name);
        }

        database_helper = new DatabaseHelper(this.getApplicationContext(),DB_name,null,DB_version);
        //database_helper.onCreate(database);
        database = database_helper.getWritableDatabase();
        database.setLocale(Locale.getDefault()); // Set the locale
        database.setLockingEnabled(true); // SQLiteDatabase is madethread-safe by using locks around critical sections.
        database_rd = database_helper.getReadableDatabase();

        Toast.makeText(this, "created database", Toast.LENGTH_SHORT).show();
        /*
        database = openOrCreateDatabase(DB_name,SQLiteDatabase.CREATE_IF_NECESSARY, null);
        database.setLocale(Locale.getDefault()); // Set the locale
        database.setLockingEnabled(true); // SQLiteDatabase is made thread-safe by using locks around critical sections.
        database.setVersion(1);

        database.execSQL(CREATE_USER_TABLE);
        */
    }

        String[] insertRecords()
        {
                Toast.makeText(this,"In insertrecords", Toast.LENGTH_SHORT).show();
                //database.openDatabase(DB_name,database.create(factory), ACCESSIBILITY_SERVICE);
                final EditText username = (EditText)findViewById(R.id.Username);
		        final EditText password = (EditText)findViewById(R.id.Password);
		        Long newUserID = 0l;
		
		        String usrname  = username.getText().toString();
		        String pwd      = password.getText().toString();
		
		        ContentValues values = new ContentValues();
		        values.put("username",usrname);
		        values.put("password",pwd);
		                //StringBuilder info = new StringBuilder();
		        String[] info = new String[10];
		
		
		        database.beginTransaction();
		        try
		        {
		                Toast.makeText(this,"Before calling inserting values", Toast.LENGTH_SHORT).show();
		                newUserID = database.insertOrThrow(TABLE_NAME, null, values);
		                Toast.makeText(this,"After calling inserting values", Toast.LENGTH_SHORT).show();
		
		                info[0] = newUserID.toString();
		                info[3] = usrname;
		                info[4] = pwd;
		
		                System.out.println(newUserID);
		                Toast.makeText(this, newUserID.toString(), Toast.LENGTH_SHORT).show();
		
		
		                Cursor c = database_rd.query(TABLE_NAME, null, null, null, null, null,null);
		                LogCursorInfo(c);
		                c.close();

        }
        catch(Exception e)
        {
                //Transaction failed;
                e.printStackTrace();
        }
        finally
        {
                database.endTransaction();
                database.close();
        }
        return info;
        }
}

